//测试用的数据
var carlist = new Array();
var old_value = "";
var currentSelIndex = -1;
var oldSelIndex = -1;

//自动完成
function AutoComplete(auto, search, event) {
	
	var keyword = $("#" + search).val();
	var autoNode = $("#" + auto);   //缓存对象（弹出框）
	if (keyword == "") {
        document.getElementById(auto).style.display = "none";
        return;
    }else {
    	if ((event.keyCode == 38 || event.keyCode == 40) 
    			&& document.getElementById(auto).style.display != "none") {
    		// 通过键盘上下键来进行选择，回车键进行确定
            var listLength = carlist.length;
            if(event.keyCode == 40 )    // 下移
            {
                if (currentSelIndex == listLength - 1) {
                    currentSelIndex = 0;
                }
                else {
                    currentSelIndex = currentSelIndex + 1;
                    if (currentSelIndex > listLength - 1) {
                        currentSelIndex = 0;
                    }
                }
            }
            if(event.keyCode == 38 )    // 上移
            {
                if (currentSelIndex == -1) {
                    currentSelIndex = listLength - 1;
                }
                else {
                    currentSelIndex = currentSelIndex - 1;
                    if (currentSelIndex < 0) {
                        currentSelIndex = listLength - 1;
                    }
                }
            }

            if (currentSelIndex != -1) {
                autoNode.children("div").eq(currentSelIndex).css("background-color", "#4778DA");
                selectedFlag = true;
                var comText = autoNode.children("div").eq(currentSelIndex).text();
                $("#" + search).val(comText);
            }
            if (oldSelIndex != -1) {
                autoNode.children("div").eq(oldSelIndex).css("background-color", "#ffffff");
            }
            oldSelIndex = currentSelIndex;
    	} else if(event.keyCode == 13){
            var comText = autoNode.hide().children("div").eq(currentSelIndex).text();
            currentSelIndex = -1;
            //文本框中的内容变成高亮节点的内容
            $("#" + search).val(comText);
            autoNode.hide();
        } else {
            var n = 0;
            old_value = $("#" + search).val();
            /**
             * 发送 ajax 获取数据
             */
            $.ajax({
                url: scholarUrl + "esFoeIndices/queryFoeIndecex",
                data: {keyword: old_value},
                dataType: "json",
                async: false,
                method: "post",
                headers: {
                    'content-type': 'application/x-www-form-urlencoded',
                    'Authorization': getLoginToken()
                },
                success: function (res) {
                    if (10000 == res.code) {
                    	carlist = res.data;
                    }
                }
            });
            
            
            if (carlist.length == 0) {
                autoNode.hide();
                return;
            }
            autoNode.empty();  //清空上次的记录
            for (i in carlist) {
                var wordNode = carlist[i].eName;   //弹出框里的每一条内容
                var esIndex = carlist[i].esIndex;
                
                var tb = "<i class='fa fa-user'></i> ";
                
                if (esIndex == "es_field") {
                	tb = "<i class='fa fa-sitemap'></i> ";
                }else if (esIndex == "es_org") {
                	tb = "<i class='fa fa-university'></i> ";
                } 
                
                
                
                var newDivNode = $("<div>").attr("id", i);    //设置每个节点的id值
                newDivNode.attr("style", "font:14px/25px arial;height:25px;padding:0 8px;cursor: pointer;color:#333");

                newDivNode.html(tb + wordNode).appendTo(autoNode);  //追加到弹出框

                //鼠标移入高亮，移开不高亮
                newDivNode.mouseover(function () {
                    if (currentSelIndex != -1) {        //原来高亮的节点要取消高亮（是-1就不需要了）
                        autoNode.children("div").eq(currentSelIndex).css("background-color", "white");
                    }
                    //记录新的高亮节点索引
                    currentSelIndex = $(this).attr("id");
                    $(this).css("background-color", "#ebebeb");
                });
                newDivNode.mouseout(function () {
                    $(this).css("background-color", "white");
                });

                //鼠标点击文字上屏
                newDivNode.click(function () {
                    //取出高亮节点的文本内容
                    var comText = autoNode.hide().children("div").eq(currentSelIndex).text();
                    currentSelIndex = -1;
                    //文本框中的内容变成高亮节点的内容
                    $("#" + search).val(comText);
                })
                if (carlist.length > 0) {    //如果返回值有内容就显示出来
                    autoNode.show();
                } else {               //服务器端无内容返回 那么隐藏弹出框
                    autoNode.hide();
                    //弹出框隐藏的同时，高亮节点索引值也变成-1
                    currentSelIndex = -1;
                }
            }
        }	
    }

    //点击页面隐藏自动补全提示框
    document.onclick = function (e) {
        var e = e ? e : window.event;
        var tar = e.srcElement || e.target;
        if (tar.id != search) {
            if ($("#" + auto).is(":visible")) {
                $("#" + auto).css("display", "none")
            }
        }
    }
}

$(function () {
   /* old_value = $("#keyword").val();
    $("#keyword").focus(function (event) {
        if ($("#keyword").val() == "") {
            AutoComplete("auto_div", "keyword", event);
        }
    });
*/
    $("#keyword").keyup(function (event) {
        AutoComplete("auto_div", "keyword", event);
    });
});